package com.gmail.avereshchaga.visual;

import com.gmail.avereshchaga.cashierdeskconnect.CashierDeskConnect;
import com.gmail.avereshchaga.models.InvoiceItemsTableModel;
import com.gmail.avereshchaga.models.InvoiceTableModel;
import com.gmail.avereshchaga.models.ItemsListTableModel;
import com.gmail.avereshchaga.models.ItemsModel;
import static com.gmail.avereshchaga.tools.Constants.ENTITY_PER_PAGE;
import com.gmail.avereshchaga.transport.Client;
import com.gmail.avereshchaga.transport.entity.Invoice;
import com.gmail.avereshchaga.transport.entity.InvoiceItems;
import com.gmail.avereshchaga.transport.entity.Item;
import com.gmail.avereshchaga.validation.AlphanumericVerifier;
import com.gmail.avereshchaga.validation.DateInputVerifier;
import com.gmail.avereshchaga.validation.NameVerifier;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd = "-//com.gmail.avereshchaga.visual//Viewer//EN",
autostore = false)
@TopComponent.Description(
    preferredID = "ViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "com.gmail.avereshchaga.visual.ViewerTopComponent")
@ActionReference(path = "Menu/Window"/*, position = 500*/)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_ViewerAction",
preferredID = "ViewerTopComponent")
@Messages({
    "CTL_ViewerAction=Viewer",
    "CTL_ViewerTopComponent=Viewer Window",
    "HINT_ViewerTopComponent=This is a Viewer window"
})
public final class ViewerTopComponent extends TopComponent {

    private Client client = new Client();
    private Integer page = 0;
    private Integer itemsPage = 0;
    private Integer itemsListPage = 0;
    private InvoiceTableModel invoiceTableModel;
    private ItemsListTableModel itemsListTableModel;
    private ItemsModel itemsModel;
    private JList list = new JList();
    private JScrollPane itemsScroll = new javax.swing.JScrollPane();
    private Invoice invoice;

    public ViewerTopComponent() {
        initComponents();
        setName(Bundle.CTL_ViewerTopComponent());
        setToolTipText(Bundle.HINT_ViewerTopComponent());
        putClientProperty(TopComponent.PROP_KEEP_PREFERRED_SIZE_WHEN_SLIDED_IN, Boolean.TRUE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jTextField5 = new javax.swing.JTextField();
        jButton7 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        jButton11 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jTextField9 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton13 = new javax.swing.JButton();
        jButton14 = new javax.swing.JButton();
        jButton15 = new javax.swing.JButton();
        jButton16 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(720, 590));
        setMinimumSize(new java.awt.Dimension(720, 590));
        setPreferredSize(new java.awt.Dimension(720, 590));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jPanel1.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel2.text")); // NOI18N

        jTextField2.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField2.text")); // NOI18N
        jTextField2.setInputVerifier(new com.gmail.avereshchaga.validation.NumericalFieldVerifier());

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel3.text")); // NOI18N

        jTextField3.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField3.text")); // NOI18N
        jTextField3.setInputVerifier(new DateInputVerifier());

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel4.text")); // NOI18N

        jTextField4.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField4.text")); // NOI18N
        jTextField4.setInputVerifier(new NameVerifier());

        jButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton2.text")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton3.text")); // NOI18N
        jButton3.setMaximumSize(new java.awt.Dimension(80, 23));
        jButton3.setMinimumSize(new java.awt.Dimension(80, 23));
        jButton3.setPreferredSize(new java.awt.Dimension(80, 23));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton2, jButton3});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jPanel2.border.title"))); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel6, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel6.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton4, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton4.text")); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton5, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton5.text")); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String[]{
                "OperationId",
                "Item name",
                "Code",
                "Item created at",
                "Item created by",
                "Price",
                "Quantity"
            }));
            jScrollPane3.setViewportView(jTable3);

            jTextField5.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField5.text")); // NOI18N

            org.openide.awt.Mnemonics.setLocalizedText(jButton7, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton7.text")); // NOI18N
            jButton7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton7ActionPerformed(evt);
                }
            });

            org.openide.awt.Mnemonics.setLocalizedText(jButton8, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton8.text")); // NOI18N
            jButton8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton8ActionPerformed(evt);
                }
            });

            org.openide.awt.Mnemonics.setLocalizedText(jButton9, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton9.text")); // NOI18N
            jButton9.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton9ActionPerformed(evt);
                }
            });

            org.openide.awt.Mnemonics.setLocalizedText(jButton10, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton10.text")); // NOI18N
            jButton10.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton10ActionPerformed(evt);
                }
            });

            org.openide.awt.Mnemonics.setLocalizedText(jButton11, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton11.text")); // NOI18N
            jButton11.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton11ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(5, 5, 5)
                    .addComponent(jLabel6)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton11)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
                    .addComponent(jButton4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton7)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton10)
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton8)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton9)
                    .addContainerGap())
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addComponent(jScrollPane3)
                    .addGap(10, 10, 10))
            );

            jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton8, jButton9});

            jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton10, jButton4, jButton7});

            jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton11, jButton5});

            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(jButton4)
                        .addComponent(jButton5)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton7)
                        .addComponent(jButton10)
                        .addComponent(jButton11))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton8)
                        .addComponent(jButton9)))
            );

            jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jPanel3.border.title"))); // NOI18N

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {null, null, null},
                    {null, null, null},
                    {null, null, null},
                    {null, null, null}
                },
                new String [] {
                    "ID", "Created at", "Created by"
                }
            ));
            jScrollPane1.setViewportView(jTable1);

            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1)
                    .addContainerGap())
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                    .addContainerGap())
            );

            jScrollPane1.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                @Override
                public void adjustmentValueChanged(AdjustmentEvent ae) {
                    int extent = jScrollPane1.getVerticalScrollBar().getModel().getExtent();
                    Integer maximumScroll = jScrollPane1.getVerticalScrollBar().getMaximum();
                    if (((jScrollPane1.getVerticalScrollBar().getValue() + extent) == maximumScroll)
                        && (client.getInvoices().size() >= ENTITY_PER_PAGE)
                        &&(getHost() != null)) {
                        page++;
                        chooseVariantsOfSearch();
                        invoiceTableModel.fireTableDataChanged();
                    }
                }
            });

            jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jPanel4.border.title"))); // NOI18N

            org.openide.awt.Mnemonics.setLocalizedText(jLabel9, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel9.text")); // NOI18N

            jTextField8.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField8.text")); // NOI18N
            jTextField8.setInputVerifier(new AlphanumericVerifier());

            org.openide.awt.Mnemonics.setLocalizedText(jLabel10, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel10.text")); // NOI18N

            jTextField9.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField9.text")); // NOI18N
            jTextField9.setInputVerifier(new DateInputVerifier());

            org.openide.awt.Mnemonics.setLocalizedText(jLabel11, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel11.text")); // NOI18N

            jTextField10.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField10.text")); // NOI18N
            jTextField10.setInputVerifier(new NameVerifier());

            jTable2.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null, null},
                    {null, null, null, null, null},
                    {null, null, null, null, null},
                    {null, null, null, null, null}
                },
                new String[]{
                    "Name",
                    "Code",
                    "Item created at",
                    "Item created by",
                    "Price",
                }));
                jScrollPane2.setViewportView(jTable2);

                org.openide.awt.Mnemonics.setLocalizedText(jButton13, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton13.text")); // NOI18N
                jButton13.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jButton13ActionPerformed(evt);
                    }
                });

                org.openide.awt.Mnemonics.setLocalizedText(jButton14, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton14.text")); // NOI18N
                jButton14.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jButton14ActionPerformed(evt);
                    }
                });

                org.openide.awt.Mnemonics.setLocalizedText(jButton15, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton15.text")); // NOI18N
                jButton15.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jButton15ActionPerformed(evt);
                    }
                });

                org.openide.awt.Mnemonics.setLocalizedText(jButton16, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jButton16.text")); // NOI18N
                jButton16.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        jButton16ActionPerformed(evt);
                    }
                });

                org.openide.awt.Mnemonics.setLocalizedText(jLabel8, org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jLabel8.text")); // NOI18N

                jTextField7.setText(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jTextField7.text")); // NOI18N
                jTextField7.setInputVerifier(new com.gmail.avereshchaga.validation.NumericalFieldVerifier());

                javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
                jPanel4.setLayout(jPanel4Layout);
                jPanel4Layout.setHorizontalGroup(
                    jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                                .addGap(44, 44, 44))
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton14)
                            .addComponent(jButton13)
                            .addComponent(jButton15)
                            .addComponent(jButton16))
                        .addGap(35, 35, 35))
                );

                jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextField10, jTextField7, jTextField8, jTextField9});

                jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton13, jButton14, jButton15, jButton16});

                jPanel4Layout.setVerticalGroup(
                    jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8)
                                .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9)
                                .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel10)
                                .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11)
                                .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton16)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(jButton13)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton14)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton15))))
                );

                jScrollPane2.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
                    @Override
                    public void adjustmentValueChanged(AdjustmentEvent ae) {
                        int extent = jScrollPane2.getVerticalScrollBar().getModel().getExtent();
                        Integer maximumScroll = jScrollPane2.getVerticalScrollBar().getMaximum();
                        if (((jScrollPane2.getVerticalScrollBar().getValue() + extent) == maximumScroll)
                            && (Client.getItemsList().size() >= ENTITY_PER_PAGE)
                            && (getHost() != null)) {
                            itemsListPage++;
                            searchItems();
                            itemsListTableModel.fireTableDataChanged();
                        }
                    }
                });

                javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
                this.setLayout(layout);
                layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap())
                );
                layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(39, Short.MAX_VALUE))
                );

                jPanel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ViewerTopComponent.class, "ViewerTopComponent.jPanel1.AccessibleContext.accessibleName")); // NOI18N
            }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
    }//GEN-LAST:event_jButton3ActionPerformed

    private void SearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SearchActionPerformed
        if (getHost() != null) {
            // reset values
            page = 0;
            jScrollPane1.getVerticalScrollBar().setValue(0);
            jTextField5.setText("");
            jTable1.clearSelection();
            Client.setInvoices(new ArrayList<Invoice>());
            Client.setInvoiceItems(new ArrayList());
            jTable3.setModel(new InvoiceItemsTableModel());

            chooseVariantsOfSearch();

            invoiceTableModel = new InvoiceTableModel();
            jTable1.setModel(invoiceTableModel);
            jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            jTable1.getSelectionModel().addListSelectionListener(new RowListener());
            if ((Client.getInvoices()).isEmpty()) {
                JOptionPane.showMessageDialog(null, "Your search returned no results", "Search result", 1);
            }
        }
    }//GEN-LAST:event_SearchActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        ArrayList<InvoiceItems> editedInvoiceItems = (ArrayList<InvoiceItems>) Client.getInvoiceItems();
        if (editedInvoiceItems != null && !(editedInvoiceItems.isEmpty())) {
            Integer rowSelected = jTable3.getSelectedRow();
            if (rowSelected != -1) {
                InvoiceItems invoiceItem = Client.getInvoiceItems().get(rowSelected);

                String quantityStr = JOptionPane.showInputDialog("Enter quantity", invoiceItem.getQuantity());
                invoiceItem.setQuantity(Long.parseLong(quantityStr));
                editedInvoiceItems.set(rowSelected, invoiceItem);

                Client.setInvoiceItems(editedInvoiceItems);
                jTable3.setModel(new InvoiceItemsTableModel());

            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        if ((Client.getInvoiceItems() != null && !(Client.getInvoiceItems().isEmpty()))
                && (getHost() != null) || invoice != null) {
            Client.setItems(new ArrayList<Item>());
            itemsPage = 0;
            ArrayList<InvoiceItems> addedInvoiceItems = (ArrayList<InvoiceItems>) Client.getInvoiceItems();
            InvoiceItems newInvoiceItem = new InvoiceItems();
            try {
                client.findAllItems(itemsPage.toString());
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            JScrollPane itemsList = createListOfItems();
            itemsList.setPreferredSize(new Dimension(50, 200));
            JOptionPane optionPane = new JOptionPane();

            String itemStr = null;
            int okCxl = JOptionPane.showConfirmDialog(null,
                    itemsList,
                    "Choose Item",
                    JOptionPane.OK_CANCEL_OPTION);
            if (okCxl == JOptionPane.OK_OPTION) {
                itemStr = (String) list.getSelectedValue();
            }
            if (itemStr == null || itemStr.equals("")) {
                return;
            }
            String quantityStr = JOptionPane.showInputDialog("Enter quantity");
            if (quantityStr == null || quantityStr.equals("")) {
                return;
            }
            newInvoiceItem.setQuantity(Long.parseLong(quantityStr));
            for (Item item : Client.getItems()) {
                if (item.getName().equals(itemStr)) {
                    newInvoiceItem.setItem(item);
                }
            }
            if (invoice != null) {
                newInvoiceItem.setInvoice(invoice);
            } else {
                Integer rowIndex = jTable1.getSelectionModel().getLeadSelectionIndex();
                newInvoiceItem.setInvoice(Client.getInvoices().get(rowIndex));
            }
            addedInvoiceItems.add(newInvoiceItem);
            Client.setInvoiceItems(addedInvoiceItems);
            jTable3.setModel(new InvoiceItemsTableModel());
        }
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed
        ArrayList<InvoiceItems> deletedInvoiceItems = (ArrayList<InvoiceItems>) Client.getInvoiceItems();
        Integer rowSelected = jTable3.getSelectedRow();
        if (rowSelected != -1) {
            InvoiceItems invoiceItem = Client.getInvoiceItems().get(rowSelected);

            invoiceItem.getItem().setName("Deleted");
            invoiceItem.setInvoice(null);

            deletedInvoiceItems.set(rowSelected, invoiceItem);

            Client.setInvoiceItems(deletedInvoiceItems);
            jTable3.setModel(new InvoiceItemsTableModel());
            jTable3.setRowSelectionInterval(jTable3.getModel().getRowCount() - 1, jTable3.getModel().getRowCount() - 1);
        }
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        if (Client.getInvoiceItems() != null && !(Client.getInvoiceItems().isEmpty())
                && (getHost() != null)) {
            try {
                client.saveInvoiceItems();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            jTable3.setModel(new InvoiceItemsTableModel());
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        if (getHost() != null) {
            Integer rowIndex = jTable1.getSelectionModel().getLeadSelectionIndex();
            if (rowIndex == -1) {
                return;
            }
            Long invoiceId = (Long) jTable1.getValueAt(rowIndex, 0);
            String invoiceIdStr = invoiceId.toString();
            jTextField5.setText(invoiceIdStr);
            try {
                client.findInvoiceItemsByInvoiceId(invoiceIdStr);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            jTable3.setModel(new InvoiceItemsTableModel());
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        if (getHost() != null) {
            Client.setInvoiceItems(new ArrayList());
            jTable3.setModel(new InvoiceItemsTableModel());
            invoice = null;
            try {
                invoice = client.createInvoice();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            if (invoice != null) {
                String invoiceNumberStr = invoice.getId().toString();
                jTextField5.setText(invoiceNumberStr);
                JOptionPane.showMessageDialog(null, "Invoice #" + invoiceNumberStr + " created", "Create operation", 1);
            } else {
                JOptionPane.showMessageDialog(null, "Creation error", "Error", 0);
            }
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        if (getHost() != null) {
            String invoiceIdDeleted = jTextField5.getText();
            if (!invoiceIdDeleted.equals("")) {
                String message = null;
                try {
                    message = client.deleteInvoice(invoiceIdDeleted);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
                JOptionPane.showMessageDialog(null, "Invoice# " + invoiceIdDeleted + message, "Delete", 1);
                Client.setInvoiceItems(new ArrayList());
                jTextField5.setText("");
                jTable3.setModel(new InvoiceItemsTableModel());
            }
        }
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton13ActionPerformed
        Integer rowSelected = jTable2.getSelectedRow();
        if (rowSelected != -1) {
            ArrayList<Item> editedItemsList = (ArrayList<Item>) Client.getItemsList();
            ArrayList<Item> itemsForEdit = (ArrayList<Item>) Client.getItemsForEdit();
            Item item = Client.getItemsList().get(rowSelected);
            String nameStr = JOptionPane.showInputDialog("Enter name", item.getName());
            item.setName(nameStr);
            String codeStr = JOptionPane.showInputDialog("Enter code", item.getCode());
            item.setCode(codeStr);
            editedItemsList.set(rowSelected, item);
            itemsForEdit.add(item);
            Client.setItemsList(editedItemsList);
            Client.setItemsForEdit(itemsForEdit);

            jTable2.setModel(new ItemsListTableModel());
        }
    }//GEN-LAST:event_jButton13ActionPerformed

    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton16ActionPerformed
        if (getHost() != null) {
            itemsListPage = 0;
            jScrollPane2.getVerticalScrollBar().setValue(0);
            jTable2.clearSelection();
            Client.setItemsList(new ArrayList<Item>());
            Client.setItemsForEdit(new ArrayList<Item>());
            searchItems();
            itemsListTableModel = new ItemsListTableModel();
            jTable2.setModel(itemsListTableModel);
            if ((Client.getItemsList()).isEmpty()) {
                JOptionPane.showMessageDialog(null, "Your search returned no results", "Search result", 1);
            }
        }
    }//GEN-LAST:event_jButton16ActionPerformed

    private void jButton15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton15ActionPerformed
        if (Client.getItemsForEdit() != null && !(Client.getItemsForEdit().isEmpty())
                && (getHost() != null)) {
            try {
                client.saveItems();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
            itemsListPage = 0;
            jScrollPane2.getVerticalScrollBar().setValue(0);
            jTable2.clearSelection();
            Client.setItemsList(new ArrayList<Item>());
            Client.setItemsForEdit(new ArrayList<Item>());
            searchItems();
            itemsListTableModel = new ItemsListTableModel();
            jTable2.setModel(itemsListTableModel);
        }
    }//GEN-LAST:event_jButton15ActionPerformed

    private void jButton14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton14ActionPerformed
        Integer rowSelected = jTable2.getSelectedRow();
        if (rowSelected != -1) {
            ArrayList<Item> deletedItemsList = (ArrayList<Item>) Client.getItemsList();
            ArrayList<Item> itemsForEdit = (ArrayList<Item>) Client.getItemsForEdit();
            Item item = Client.getItemsList().get(rowSelected);
            item.setName("Deleted");
            deletedItemsList.set(rowSelected, item);
            itemsForEdit.add(item);
            Client.setItemsList(deletedItemsList);
            Client.setItemsForEdit(itemsForEdit);
            jTable2.setModel(new ItemsListTableModel());
        }
    }//GEN-LAST:event_jButton14ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton13;
    private javax.swing.JButton jButton14;
    private javax.swing.JButton jButton15;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    public void chooseVariantsOfSearch() {
        String idStr = jTextField2.getText();
        String createdDate = jTextField3.getText();
        String createdBy = jTextField4.getText();
        if (idStr.equals("") && createdDate.equals("") && createdBy.equals("")) {
            try {
                client.findAllInvoices(page.toString());
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        } else if (!idStr.equals("")) {
            try {
                client.findInvoiceById(idStr);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        } else if (!createdDate.equals("")) {
            try {
                client.findInvoicesByFilter(page.toString(), createdDate, createdBy);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        } else {
            try {
                client.findInvoicesByName(page.toString(), createdBy);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }

    public void searchItems() {
        String code = jTextField7.getText();
        String name = jTextField8.getText();
        String createdAt = jTextField9.getText();
        String createdBy = jTextField10.getText();
        try {
            client.findItemsByFilter(code, name, createdAt, createdBy, itemsListPage.toString());
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    public JScrollPane createListOfItems() {

        itemsModel = new ItemsModel();
        list = new JList(itemsModel);
        itemsScroll = new JScrollPane(list);
        itemsScroll.getVerticalScrollBar().addAdjustmentListener(new ScrollListener());
        return itemsScroll;
    }

    private class RowListener implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent event) {
            if (event.getValueIsAdjusting()) {
                return;
            }
            Integer rowIndex = jTable1.getSelectionModel().getLeadSelectionIndex();
            if (rowIndex == -1) {
                return;
            }
            invoice = null;
            Long invoiceId = (Long) jTable1.getValueAt(rowIndex, 0);
            String invoiceIdStr = invoiceId.toString();
            jTextField5.setText(invoiceIdStr);
            if (getHost() != null) {
                try {
                    client.findInvoiceItemsByInvoiceId(invoiceIdStr);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            jTable3.setModel(new InvoiceItemsTableModel());
        }
    }

    private class ScrollListener implements AdjustmentListener {

        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            int extent = itemsScroll.getVerticalScrollBar().getModel().getExtent();
            Integer maximumScroll = itemsScroll.getVerticalScrollBar().getMaximum();
            if (((itemsScroll.getVerticalScrollBar().getValue() + extent) == maximumScroll)
                    && (Client.getItems().size() >= ENTITY_PER_PAGE
                    && (getHost() != null))) {
                itemsPage++;
                try {
                    client.findAllItems(itemsPage.toString());
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }
                itemsModel.fireContentsChanged(this, itemsPage * ENTITY_PER_PAGE + 1, (itemsPage + 1) * ENTITY_PER_PAGE);
            }
        }
    }

    public String getHost() {
        String host = null;
        for (CashierDeskConnect cashierDeskConnect : Lookup.getDefault().lookupAll(CashierDeskConnect.class)) {
            host = cashierDeskConnect.getHost();
        }
        return host;
    }
}
